package service.impl.v2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.v2.ConfigVOMapper;
import net.sf.json.JSONObject;
import service.v2.MemberService;
import service.v2.WeChatService;
import util.WeixinUtil;
@Service
public class WeChatServiceImpl implements WeChatService {
	@Autowired
	private ConfigVOMapper configvomapper;
	
	@Autowired
	private MemberService memberService;
	
	public String getWeChatNickName(String access_token, String openid) {
		
		String nickname = WeixinUtil.getWeChatInfo(access_token,openid);
		
		return nickname;
	}

	public String getAccessToken() {
		String confValue = configvomapper.loadConfig("ACCESS_TOKEN");
		JSONObject conf =JSONObject.fromObject(confValue);
		String access_token = conf.getString("token");
		return access_token;
	}
	public void sendWeChatMsgCommonMethod(String member_id,String content) {

        //获取ACCESS_TOKEN
		String access_token = getAccessToken() ;
        //获取openid
		String openid = memberService.selectMemberOpenid(member_id);
		if(openid !=null){
			System.out.println(content);
			System.out.println("openid="+openid);
			WeixinUtil.sendMsgText(openid,content,access_token);
			
		}
		
	}
	public void sendWeChatMsgCommonMethod1(String openId,String content) {
		
		if(openId !=null){
			//获取ACCESS_TOKEN
			String access_token = getAccessToken() ;
	        
		    WeixinUtil.sendMsgText(openId,content,access_token);
		}
		
	}
	public void sendWeChatImgCommonMethod(String member_id,String media_id) {
		
		//获取openid
		String openid = memberService.selectMemberOpenid(member_id);
		if(openid !=null){
			//获取ACCESS_TOKEN
			String access_token = getAccessToken() ;
			WeixinUtil.sendMsgImg(openid,media_id,access_token);
		}
		
	}
	public void sendWeChatImgCommonMethod1(String openId,String media_id) {

		if(openId !=null){
			//获取ACCESS_TOKEN
			String access_token = getAccessToken() ;
		    WeixinUtil.sendMsgImg(openId,media_id,access_token);
		}
		
	}

}
